package cn.boyu.service.impl;

import cn.boyu.entity.AssetsAmmeter;
import cn.boyu.entity.AssetsTerminal;
import cn.boyu.entity.SysDictData;
import cn.boyu.exception.BusinessException;
import cn.boyu.mapper.AssetsTerminalMapper;
import cn.boyu.query.TerminalQuery;
import cn.boyu.service.IAssetsTerminalService;
import cn.boyu.service.ISysDictDataService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yn
 * @since 2022-11-22
 */
@Service
public class AssetsTerminalServiceImpl extends ServiceImpl<AssetsTerminalMapper, AssetsTerminal> implements IAssetsTerminalService {
    @Autowired
    private AssetsTerminalMapper assetsTerminalMapper;

    @Autowired
    private ISysDictDataService dataService;

    @Override
    public IPage<AssetsTerminal> queryPage(TerminalQuery qo) {
        IPage<AssetsTerminal> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        QueryWrapper<AssetsTerminal> qw = new QueryWrapper<>();
        qw.eq(qo.getTerminalId() != null, "terminal_id", qo.getTerminalId());
        qw.eq("del_flg", 0);
        IPage<AssetsTerminal> result = page(page, qw);
        for (AssetsTerminal terminal : result.getRecords()) {
            List<SysDictData> companyType = dataService.getType("companyType", terminal.getCompanyType());

            if (companyType.size() != 0) {
                terminal.setCompanyTypeString(companyType.get(0).getDictLabel());
            }
        }
        System.out.println(result);
        return result;
    }

    @Override
    public void saveitions(Long firstId, Long lastId, AssetsTerminal assetsTerminal) {
        if (firstId != null && lastId != null && firstId <= lastId) {
            for (long i = firstId; i <= lastId; i++) {
                assetsTerminal.setTerminalId(i);
                save(assetsTerminal);
            }
        } else {
            throw new BusinessException("终端号不符合规范");
        }
    }

    @Override
    public void batchDelete(Long[] ids) {
        assetsTerminalMapper.batchDelete(ids);
    }

    @Override
    public List<AssetsTerminal> listTerminal() {
        QueryWrapper<AssetsTerminal> qw = new QueryWrapper<>();
        qw.eq("del_flg", 0);
        qw.eq("mount_flg", 0);
        List<AssetsTerminal> terminals = assetsTerminalMapper.selectList(qw);
        return terminals;
    }
}
